package com.lanling.eduservice.controller.front;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lanling.eduservice.client.OrdersClient;
import com.lanling.eduservice.entity.EduCourse;
import com.lanling.eduservice.entity.chapter.ChapterVo;
import com.lanling.eduservice.entity.frontvo.CourseFrontInfoVo;
import com.lanling.eduservice.entity.frontvo.CourseWebVo;
import com.lanling.eduservice.orther.CourseWebVoOrder;
import com.lanling.eduservice.orther.R;
import com.lanling.eduservice.service.EduChapterService;
import com.lanling.eduservice.service.EduCourseService;
import com.lanling.eduservice.utils.JwtUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * @author 胡振华
 * @date 2020-11-26 9:11
 */
@RestController
@RequestMapping("/eduservice/coursefront")
@CrossOrigin
public class CourseFrontController {

    @Autowired
    private EduCourseService eduCourseService;

    @Autowired
    private EduChapterService eduChapterService;

    @Autowired
    private OrdersClient ordersClient;

    @PostMapping("getCoursePageList/{page}/{limit}")
    public R getCoursePageList(@PathVariable long page, @PathVariable long limit, @RequestBody(required = false)CourseFrontInfoVo courseFrontInfoVo){
        Page<EduCourse> eduCoursePage = new Page<>(page,limit);
        Map<String,Object> map = eduCourseService.getCoursePageList(eduCoursePage,courseFrontInfoVo);
        return R.ok().data(map);
    }

    //根据课程id查询课程详细信息
    @GetMapping("getCourseFrontInfo/{id}")
    public R getCourseFrontInfo(@PathVariable String id, HttpServletRequest request){
        //根据课程id查询课程基本信息
        CourseWebVo courseWebVo = eduCourseService.getBaseCourseInfo(id);

        //根据课程id查询课程章节和小节信息
        List<ChapterVo> chapterVos = eduChapterService.getAllChapter(id);

        boolean buyCourse = false;
        String memberIdByJwtToken = JwtUtils.getMemberIdByJwtToken(request.getHeader("usertoken"));
        if (!StringUtils.isEmpty(memberIdByJwtToken)){
            buyCourse = ordersClient.isBuyCourse(id, memberIdByJwtToken);
        }
        return R.ok().data("eduCourse",courseWebVo).data("chapterVos",chapterVos).data("isBuy",buyCourse);
    }

    //根据课程id查询课程信息
    @PostMapping("getCourseInfoOrder/{id}")
    public CourseWebVoOrder getCourseInfoOrder(@PathVariable String id) {
        CourseWebVo courseInfo = eduCourseService.getBaseCourseInfo(id);
        CourseWebVoOrder courseWebVoOrder = new CourseWebVoOrder();
        BeanUtils.copyProperties(courseInfo,courseWebVoOrder);
        return courseWebVoOrder;
    }

    @GetMapping("searchCourse/{coursename}")
    public R searchCourse(@PathVariable String coursename){
        List<EduCourse> eduCourses = eduCourseService.searchCourse(coursename);
        return R.ok().data("courses",eduCourses);
    }

    //课程购买数加1
    @GetMapping("addCourseBuyCount/{courseid}")
    public R addCourseBuyCount(@PathVariable String courseid){
        eduCourseService.addCourseBuyCount(courseid);
        return R.ok();
    }


}